package dhm.com.dhmshop.view.mine.myshop;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.mine.SeeCuiAdapter;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.SeecuiBean;
import dhm.com.dhmshop.utils.SpUtils;

public class SeecuiActivity extends BaseActiity implements LoginContract.IView {
    @BindView(R.id.recy)
    XRecyclerView recy;
    private PressenterImpl pressenter;
    private String shop_id;
    private int page = 1;
    private List<SeecuiBean.DataBean> seecuiBeanData;
    private SeeCuiAdapter seeCuiAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_seecui;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        page=1;
        shop_id = SpUtils.getString(this, "shop_id");
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                search();
            }

            @Override
            public void onLoadMore() {
                search();
            }
        });

    }

    @Override
    protected void initData() {
        search();

    }

    private void search() {

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("shop_id", shop_id);
        map.put("page", page + "");
        pressenter.sendMessage(SeecuiActivity.this, Constant.SendOrder, map, SeecuiBean.class);
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof SeecuiBean) {
            SeecuiBean seecuiBean = (SeecuiBean) data;
            if (seecuiBean.getCode() == 1) {
                if (page==1){
                    seecuiBeanData = seecuiBean.getData();
                    seeCuiAdapter = new SeeCuiAdapter(this);
                    recy.setAdapter(seeCuiAdapter);
                }else {
                    seecuiBeanData.addAll(seecuiBean.getData());
                }
                page++;
                seeCuiAdapter.showImage(seecuiBeanData);
            }else {
                Toast.makeText(this, seecuiBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            if (seecuiBeanData==null||seecuiBeanData.size()==0){
                recy.setVisibility(View.GONE);
            }
            recy.loadMoreComplete();
            recy.refreshComplete();
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
